var linkFilter;


/*
 * ON LOAD DA PAGINA
 */
$(document).ready(function()
{
	if( $('.zoom-images').length ) mountImageZoomGallery();
	if( $('.carrossel-home').length ) startCarrosselHome();
	
	$('select.shippingType').change( getShipingInfo );
	$('select.colors').change( changeProduct );
	$('.sizes li').click( changeSizes );
	
	twitterButton();
	
	$('.homeGallery a').live('click',getHomeProducts);
	$('.filters a').live('click',getProducts);
	$('select.flagOptions').change( getProducts );
	$('select.themeOptions').change( getProducts );
	
	$('.showCart a').live('click',showShoppingCart);
	$('.closeCart').live('click',hideShoppingCart);
	$('.removeItemCart a').live('click',removeItemShoppingCart);
	$('.addItemCart a').live('click',addItemShoppingCart);
	
	if( $('#cadastro-form').length >= 1 )
	{
		changeOptForm();
		$('.clientOption input').live('click',changeOptForm);
	}
	
	linkFilter = $('.filterAll').attr('href');
	
	$('.formNewsLetter').submit(sendNewsLetter);
	
	$('#postalCode').focusout(getAddress);
	$('#postalCode').mask( '99999-999' );
	$('#areaCode').mask( '99' );
	$('#phoneNumber').mask( '99999999' );
});


$(window).scroll(function()
{
	
});

function getProducts()
{
	linkFilter = ( $(this).attr('href') != undefined ? $(this).attr('href') : linkFilter );
	var url = linkFilter + '/' + $('#flagOptions').val() + '/' + $('#themeOptions').val() + '/50/true';
	$('.titleHighlight').addClass( 'loaderGallery' );
	$('.titleHighlight').after( '<span style="clear: both;display: block;"></span>' );
	$.get( url, function(data) { $('#section').html( data )} );
	
	return false;
}

function getHomeProducts()
{
	linkFilter = ( $(this).attr('href') != undefined ? $(this).attr('href') : linkFilter );
	var url = linkFilter + '/0/0/0/5/true';
	$('.homeGallery li').removeClass( 'active' );
	$(this).parent('li').addClass( 'active' );
	
	 $('.ajaxRequest').html( '<span class="loader"></span>' );
	
	$.get( url, function(data) { $('.ajaxRequest').html( data )} );
	
	return false;
}

var crHomeInterval;
var crHomeLimit;
var crHomePosit = 0;
function startCarrosselHome()
{
	$('.carrossel-home .steps li:eq(0)').show();
	crHomeLimit = $('.carrossel-home .steps li').length;
	crHomeInterval = setInterval("changeCarrosselHome()",5000);
}

function changeCarrosselHome()
{
	crHomePosit++
	if( crHomePosit == crHomeLimit ) crHomePosit = 0;
	$('.carrossel-home .steps li').hide();
	$('.carrossel-home .steps li:eq('+crHomePosit+')').fadeIn('slow');
	
	$('.carrossel-home .naveg span').removeClass('active');
	$('.carrossel-home .naveg span:eq('+crHomePosit+')').addClass('active');
}

function startJqzoom()
{
	$('.darkenScreen').fadeTo( 0,.65 );
	$('.zoom-images').show();
	$('.zoom-images .imagesZoom').height( $(window).height() > '1400' ? '1380' : ($(window).height()-40) )
	
	$('.jqzoom').jqzoom({
		zoomType      : 'innerzoom',
		title         : false,
		preloadImages : false,
		alwaysOn      : true
	});
	
	$('.sideBarImages').css({
		width      : $('.zoom-images .imagesZoom').width() + 'px',
		marginLeft : -($('.zoom-images .imagesZoom').width() / 2) + 'px'
	});
	$('.zoomPad').css({
		marginTop  : -($('.zoom-images .imagesZoom').height() / 2) + 'px',
		marginLeft : -($('.zoom-images .imagesZoom').width() / 2) + 'px'
	});
	
	
	$('.zoomPad').prepend( $('.sideBarImages') );
}

function closeZoom()
{
	$('.zoom-images').hide();
	
	return false;
}

function changeBigImage()
{
	$('.image img').hide();
	$('.image img').attr('src', $(this).attr('src') );
	$('.image img').fadeIn();
	
	return false;
}

function changeVeryBigImage()
{
	$('.zoomWrapperImage img').attr('src', $(this).attr('id') );
	
	return false;
}

function mountImageZoomGallery()
{
	var html;
	var images = '';
	var obj = jQuery.parseJSON( $('.zoom-images').html() );
	
	if( obj.files.length )
	{
		for(var i = 0; i < obj.files.length; i++ )
			images += '<p class="changeVeryBigThumb" id="'+obj.bigPath+'/'+obj.files[i].file+'"><img src="'+obj.smallPath+obj.files[i].file+'" alt="-" width="90" class="imgBigThumb"></p>';
			
		html = '<li class="darkenScreen"></li>' +
				'<li class="sideBarImages museo">' +
					'<p class="closeZoom">Fechar</p>' + images +
				'</li>' + 
				'<li>' +
					'<a href="'+obj.bigPath+'/'+obj.files[0].file+'" title="-" class="jqzoom" >' +
						'<img src="'+obj.bigPath+'/'+obj.files[0].file+'" title="-" class="imagesZoom">' +
					'</a>' +
				'</li>';
		
		$('.zoom-images').html( html );
		$('.zoom').click( startJqzoom );
		$('.closeZoom').click( closeZoom );
		$('.changeBigThumb').click( changeBigImage );
		$('.changeVeryBigThumb').click( changeVeryBigImage );
	}
}

function changeProduct()
{
	window.location.href = $(this).val();
}

function changeSizes()
{
	$('.sizes li').removeClass( 'active' )
	$(this).addClass( 'active' );
	$('.sizeDescription').html( $(this).find('em').text() );
}

function twitterButton()
{
	!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
}

function showShoppingCart()
{
	var url = $(this).attr('href');
	$.get(url, function(data) 
	{
		$('#ajaxCart').html(data);
	});
	
	return false;
}

function hideShoppingCart()
{
	$('#ajaxCart').html('');
	
	return false;
}

function addItemShoppingCart()
{
	var url = $(this).attr('href');
	
	$.get(url, function(data) 
	{
		$('#ajaxCart').html(data);
	});
	
	return false;
}

function removeItemShoppingCart()
{
	var url = $(this).attr('href');
	var obj = $(this);
	
	$.get(url, function(data) 
	{
		$('.showCart a').text('Carrinho (' +$(data).find('tr').length+ ')');
		obj.parent('td').parent('tr').remove();
	});
	
	return false;
}

function changeOptForm()
{
	var actionForm = $('#cadastro-form').attr('action');
	var item = $('#cadastro-form .clientOption input:eq(0)').attr('checked');
	
	
	if( item == false)
	{
		$('#cadastro-form').attr( 'action', actionForm.replace('save','login') );
		
		$('.name-label').hide();
		$('.name-object').hide();
		$('.areaCode-label').hide();
		$('.areaCode-object').hide();
		$('.phoneNumber-label').hide();
		$('.phoneNumber-object').hide();
		$('.street-label').hide();
		$('.street-object').hide();
		$('.number-label').hide();
		$('.number-object').hide();
		$('.complement-label').hide();
		$('.complement-object').hide();
		$('.district-label').hide();
		$('.district-object').hide();
		$('.postalCode-label').hide();
		$('.postalCode-object').hide();
		$('.city-label').hide();
		$('.city-object').hide();
		$('.state-label').hide();
		$('.state-object').hide();
		$('.dt-title').hide();
		$('.dl-wp10').hide();
	}
	else
	{
		$('#cadastro-form').attr( 'action', actionForm.replace('login','save') );
		
		$('.name-label').show();
		$('.name-object').show();
		$('.email-label').show();
		$('.email-object').show();
		$('.password-label').show();
		$('.password-object').show();
		$('.areaCode-label').show();
		$('.areaCode-object').show();
		$('.phoneNumber-label').show();
		$('.phoneNumber-object').show();
		$('.shippingType-label').show();
		$('.shippingType-object').show();
		$('.street-label').show();
		$('.street-object').show();
		$('.number-label').show();
		$('.number-object').show();
		$('.complement-label').show();
		$('.complement-object').show();
		$('.district-label').show();
		$('.district-object').show();
		$('.postalCode-label').show();
		$('.postalCode-object').show();
		$('.city-label').show();
		$('.city-object').show();
		$('.state-label').show();
		$('.state-object').show();
		$('.dt-title').show();
		$('.dl-wp10').show();
	}
}

function getAddress()
{
	var cep = $(this).val();
	var url = 'http://' + location.hostname + '/cadastro/get-address/cep/' + cep
	
	$.getJSON(url, function(data) 
	{
		var address = data.cep[0].tp_logradouro + ' ' + data.cep[0].logradouro;
		var neighborhood = data.cep[0].bairro;
		var state = data.cep[0].uf_sigla;
		var city = data.cep[0].cidade;
		
		$('#street').val( address );
		$('#district').val( neighborhood );
		$('#state').val( state );
		$('#city').val( city );
	});
}

function getShipingInfo()
{
	$('#infoCorreio').remove();
	$('.shippingType-object').after( '<div id="infoCorreio" class="infoLoader">calculando frete...<div>' );
	
	var url = 'http://' + window.location.hostname + '/correios/' + $('#postalCode').val().replace('-','') + '/' +  $('#shippingType').val();
	
	$.get(url, function(data) 
	{
		$('#infoCorreio').remove();
		$('.shippingType-object').after( '<div id="infoCorreio">'+data+'<div>' );
	});
	
	return false;
}

function sendNewsLetter()
{
	var padrao = /^([\w-]+(\.[\w-]+)*)@(( [\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(\.[a-z]{2})?)$/i;
	var action = $('.formNewsLetter').attr('action') + 'ajax/true'; 
	var email = $('.formNewsLetter .email input').val();
	
	if( !padrao.test( email ) ) $('.formNewsLetter').after( '<p class="error">informe um e-mail válido.</p>' );
	else $.post(action, 'email=' + email , function(data) {	
		$('.newsLetter').html(data);
	});
	
	return false;
}
























